package projet.camping.sevlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import projet.camping.exception.ArgumentInvalideException;
import projet.camping.exception.PartieNonExistantException;
import projet.camping.usecases.GestionPartie;

@WebServlet(urlPatterns = "/pret.html")
public class Ready extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@EJB
	GestionPartie uccPartie;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.sendRedirect(resp.encodeRedirectURL("campingBattle.jsp"));
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		HttpSession session = req.getSession();
		PrintWriter out = resp.getWriter();
		String nomPartie = (String) session.getAttribute("nomPartie");
		String nomUser = (String) session.getAttribute("login");
		System.out.println("nom de la partie: "
				+ nomPartie);
		System.out.println("nom partie session : "
				+ nomUser);

		try {
			if (uccPartie.demarrerPartie(nomPartie, nomUser)) {
				System.out.println("ready");
				out.write("ready");
			}else{
				System.out.println("not ready");
				out.write("notReady");
			}
		} catch (PartieNonExistantException e) {
			out.write("partieNonExistante");
			System.err.println("la partie n'existe pas");
		} catch (ArgumentInvalideException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
